﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.DB;
using MaxLeaf.Common;

namespace MaxLeaf.DAL.K2K
{
    public class t_ItemDAL:BaseDAL
    {
        string _strFParentID = null;
        public t_ItemDAL(string strfitemClassID, string strtmpUpdateFNumberTableName)
            : base()
        {
            strTableName = "t_Item";
            strUpdateFNumberTableName = strtmpUpdateFNumberTableName;

            mblnNeedEditKeyValue = false;
            strFieldNames = "FItemClassID,FParentID,FLevel,FName,FShortNumber,FFullNumber,FDetail,UUID,FDeleted,FNumber";

            strFItemClassID = strfitemClassID;
        }
        public string GetFItemId(DB.DBAccess dbOrgK3Con, DB.DBAccess dbDestK3Con, int intFLevel, string strOrgFParentID, string strFParentID, string strFNumber, int intFDetail)
        {
            _strFParentID = strFParentID;
            dictOtherKeyFieldsCond = new Dictionary<string, object>() {  { "FItemClassID", strFItemClassID }, { "FLevel", intFLevel.ToString() }, { "FDetail", intFDetail} };
            if (strOrgFParentID.Length > 0)
            {
                dictOtherKeyFieldsCond.Add( "FParentID", strOrgFParentID );
            }
            dictDestOtherKeyFieldsCond = new Dictionary<string, object>() { { "FParentID", strFParentID }, { "FItemClassID", strFItemClassID }, { "FLevel", intFLevel.ToString() }, { "FDetail", intFDetail } };

            Dictionary<string,object>  dictOrg=new   Dictionary<string,object> (){
                { "FItemClassID", strFItemClassID }, {"FNumber",strFNumber}
            };
            if (strOrgFParentID.Length > 0)
            {
                dictOrg.Add( "FParentID", strOrgFParentID );
            }
            DataRow drOrgData= dbOrgK3Con.GetTableDataFirstRow(strTableName,  dictOrg
            );

            Update(dbOrgK3Con, dbDestK3Con,CvtFunc.ToString (drOrgData["FitemID"]));
            
            DataTable dtData = dbDestK3Con.GetTableData(strTableName, new Dictionary<string, object>() { { "FItemClassID", strFItemClassID }, { "FNumber", strFNumber } }, "FitemID");

            //触发器fullname
            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "FItemClassID", strFItemClassID }, { "FitemID", dtData.Rows[0]["FitemID"] } });
            dbDestK3Con.ExecuteSql(string.Format("update t_Item set FName=FName where {0}",kvp.Key ),kvp.Value );

            return CvtFunc.ToString(dtData.Rows[0]["FitemID"]);
        }

        public override void Delete(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strKeyValue)
        {
            throw new MaxLeafException("Delete t_Item 没实现");
        }
        public override int  Update(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strKeyValue)
        {
            if (dictOtherKeyFieldsCond == null)
            {
                throw new MaxLeafException("Update t_Item 缺少 dictOtherKeyFieldsCond");
            }
            return base.Update(dbOrgK3Con, dbDestK3Con, strKeyValue);
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FParentID", StringComparison.InvariantCultureIgnoreCase))
            {

                drDestData[strFieldName] = _strFParentID;
                
            }
            
            else
            {
                base.DAL_GetFieldData(dbOrgK3Con, dbDestK3Con, strFieldName, drOrgData, drDestData);
            }
        }

    }
}
